package com.tanhua.server.controller;

import com.tanhua.commons.utils.UserHolder;
import com.tanhua.model.domain.mysql.Settings;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.service.LoginService;
import com.tanhua.server.service.SettingsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private LoginService loginService;

    /**
     * description: 通用设置查询 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:47 <br>
     * author: SangWu <br>
     *
     * @param
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping("/settings")
    public ResponseEntity settings() {
        SettingsVo vo = settingsService.settings();
        return ResponseEntity.ok(vo);
    }

    /**
     * description: 设置陌生人问题 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 21:01 <br>
     * author: SangWu <br>
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/questions")
    public ResponseEntity saveQuestions(@RequestBody Map<String, String> map) {
        String content = map.get("content");
        settingsService.saveQuestions(content);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 通知设置 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 21:04 <br>
     * author: SangWu <br>
     *
     * @param settings
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity saveNotificationsSettings(@RequestBody Settings settings) {
        settingsService.saveNotificationsSettings(settings);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 黑名单查询 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 21:08 <br>
     * author: SangWu <br>
     *
     * @param page
     * @param pagesize
     * @return org.springframework.http.ResponseEntity
     */
    @GetMapping("/blacklist")
    public ResponseEntity settings(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize) {

        PageResult pageResult = settingsService.findBlacklist(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * description: 移除黑名单 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 21:19 <br>
     * author: SangWu <br>
     *
     * @param blackUserId
     * @return org.springframework.http.ResponseEntity
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteFromBlackList(@PathVariable("uid") Long blackUserId) {
        settingsService.deleteFromBlackList(blackUserId);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 更改手机号,发送验证码 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:28 <br>
     * author: SangWu <br>
     *
     * @param
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode() {
        String mobile = UserHolder.getMobile();
        loginService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /**
     * description: 手机号更改,验证码效验 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:30 <br>
     * author: SangWu <br>
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity updatePhoneOfcheckCode(@RequestBody Map<String, String> map) {
        String verificationCode = map.get("verificationCode");
        Boolean verification = settingsService.updatePhoneOfcheckCode(verificationCode);
        HashMap<String, Boolean> resMap = new HashMap<>(2);
        resMap.put("verification",verification);
        return ResponseEntity.ok(resMap);
    }

    /**
     * description: 更改手机号 <br>
     * version: 1.0 <br>
     * date: 2021/11/14 20:44 <br>
     * author: SangWu <br>
     *
     * @param map
     * @return org.springframework.http.ResponseEntity
     */
    @PostMapping("/phone")
    public ResponseEntity updatePhone(@RequestBody Map<String, String> map) {
        String phone = map.get("phone");
        settingsService.updatePhone(phone);
        return ResponseEntity.ok(null);
    }
}